﻿@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="appUser" style="margin:2px;padding:2px;">
    <el-form inline label-position="right" label-width="70px" class="query-form">
        <el-form-item label="用户名称">
            <el-input placeholder="请输入用户名称" style="width:150px;" v-model="userName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
            <el-select v-model="enabled">
                <el-option label="全部" value="null"></el-option>
                <el-option label="启用" value="true"></el-option>
                <el-option label="禁用" value="false"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="success" size="small" @@click="userEdit(null)" v-show="pageButtons.includes('adduser')">新增</el-button>
            <el-button type="primary" size="small" @@click="handleSearch">查询</el-button>
        </el-form-item>
    </el-form>
    <el-table :data="tableData" stripe border style="width:100%" size="mini">
        <el-table-column type="index" width="50" align="center"></el-table-column>
        <el-table-column prop="userName" label="用户名称" width="180"></el-table-column>
        <el-table-column prop="realName" label="真实名称" width="180"></el-table-column>
        <el-table-column label="状态" width="100" align="center">
            <template slot-scope="scope">
                <el-button type="success" round size="mini" v-if="scope.row.enabled==true">{{scope.row.enabled === false ? '禁用' : '启用'}}</el-button>
                <el-button type="danger" round size="mini" v-if="scope.row.enabled==false">{{scope.row.enabled === false ? '禁用' : '启用'}}</el-button>
            </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200" align="center">
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="200" align="center">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="450" align="center">
            <template slot-scope="scope">
                <el-button :type="scope.row.enabled === true?'danger':'success'" @@click="SetEnabled(scope.row)" size="mini"  v-show="pageButtons.includes('user_setenabled')">
                    <span>{{scope.row.enabled === true ? '禁用' : '启用'}}</span>
                </el-button>
                <el-button type="primary" size="mini" @@click="userEdit(scope.row )"  v-show="pageButtons.includes('edituser')">修改</el-button>
                <el-button type="info" size="mini" @@click="bandRoles(scope.row )"  v-show="pageButtons.includes('bandrole')">绑定角色</el-button> 
            </template>
        </el-table-column>
    </el-table>
    <el-pagination @@size-change="handleSizeChange"
                   @@current-change="handleCurrentChange"
                   :current-page="currentPage"
                   :page-sizes="[10, 20, 50, 100]"
                   :page-size="10"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="total">
    </el-pagination>

    <el-dialog title="绑定角色"
               :visible.sync="dialogVisible"
               width="30%">
        <el-checkbox-group v-model="roleIds">
            <template v-for="item in roles">
                <el-checkbox :label="item.roleId">{{item.roleName}}</el-checkbox>
            </template>
        </el-checkbox-group>
        <span slot="footer" class="dialog-footer">
            <el-button @@click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @@click="SaveUserRoles()">确 定</el-button>
        </span>
    </el-dialog> 
</div>

<script>
    function refreshFromChild() {
        vueUser.GetUserList();
    }

    var vueUser = new Vue({
        el: '#appUser',
        data() {
            return {
                pageButtons:[],

                roleIds:[],
                dialogVisible:false,
                userName: '',
                enabled:'null',
                pageSize: 10,
                currentPage: 1,
                total: 0,
                tableData: [],
                roles: [],
                selectUserId: 0,
                 


            }
        },
        created(){
            var _this = this;
            _this.GetUserList();
            _this.$getMenuButtons();
             
        }, 
        methods: {  
            SaveUserRoles: function () {
                var _this = this;
                _this.$post("@Url.Action("BandUserRoles", "User")", {
                    userId: _this.selectUserId,
                     roleIds: _this.roleIds,
                 }).then((res) => {
                     _this.$success(res.message, function () {
                         _this.dialogVisible = false;
                         _this.GetUserList();
                     });
                  }).catch((error) => {
                  });
            },
            getUserBandRoles: function (userId) {
                var _this = this;
                _this.$get("@Url.Action("GetUserBandRoles", "User")?userId=" + userId).then((response) => {
                    _this.roleIds = response;
                  }).catch((error) => {
                  });
            },
            bandRoles: function (row) {
                this.roleIds = [];
                this.getSimpleRoles();
                this.dialogVisible = true;
                this.selectUserId = row.id;
                this.getUserBandRoles(row.id);
            },
            getSimpleRoles() {
                 var _this = this;
                 _this.$get("@Url.Action("GetSimpleRoleList", "Role")"  ).then((response) => {
                     _this.roles = response;
                  }).catch((error) => {
                  });
            },
            userEdit(row) {
                var id = 0;
                if (row) id = row.id;
                this.$addTab(id, id == 0 ? "新增项目" : "修改项目", "../user/edit?id=" + id);
            }, 
            tableRowClassName({ row, rowIndex }) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            handleSearch() {
                this.GetUserList();
            },
            handleClick(row) {
                console.log(row);
            },
            GetUserList() {
                  var _this = this;
                 _this.$post("@Url.Action("GetUserPageList", "user")", {
                     PageIndex: _this.currentPage,
                     PageSize: _this.pageSize,
                     userName: _this.userName,
                     enabled: _this.enabled == 'null' ? null : _this.enabled == "true" ? true : false,
                     order: [ 
                         { FieldName: "Id", Order: "Desc" }
                     ]
                 }).then((response) => {
                        _this.tableData = response.items;
                        _this.total = response.totalCount;
                  }).catch((error) => {
                  });
            },
            SetEnabled(row) {
                  var _this = this;
                  axios.post("@Url.Action("SetEnabled", "user")", {
                      UserId: row.id,
                      Enabled: !(row.enabled)
                  }).then((res) => {
                      _this.$success(res.message, function () {
                          if(res.success)
                              _this.GetUserList();
                      });


                  }).catch((error) => {
                  });
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.GetUserList();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.GetUserList();
            }
        }
    });
</script>
